package cn.jbt.api.admin.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import lombok.experimental.Accessors;
import org.hibernate.annotations.ColumnDefault;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.Date;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@EntityListeners(AuditingEntityListener.class)
@Entity
@Table(name = "xxl_job_info", schema = "jbt")
public class XxlJobInfo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Integer id;

    @NotNull(message = "执行器不能为空")
    @Min(value = 1, message = "执行器'值'错误")
    @Column(name = "job_group", nullable = false)
    private Integer jobGroup;

    @NotBlank(message = "任务描述不能为空")
    @Size(max = 255)
    @Column(name = "job_desc", nullable = false)
    private String jobDesc;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "add_time")
    private Date addTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "update_time")
    private Date updateTime;

    @NotBlank(message = "负责人不能为空")
    @Size(max = 64)
    @Column(name = "author", length = 64)
    private String author;

    @Size(max = 255)
    @Column(name = "alarm_email")
    private String alarmEmail = "";

    @NotBlank(message = "调度类型不能为空")
    @Size(max = 50)
    @ColumnDefault("'NONE'")
    @Column(name = "schedule_type", nullable = false, length = 50)
    private String scheduleType;

    @NotBlank(message = "调度配置不能为空")
    @Size(max = 128)
    @Column(name = "schedule_conf", length = 128)
    private String scheduleConf;

    @NotBlank(message = "调度过期策略不能为空")
    @Size(max = 50)
    @ColumnDefault("'DO_NOTHING'")
    @Column(name = "misfire_strategy", nullable = false, length = 50)
    private String misfireStrategy;

    @NotBlank(message = "路由策略不能为空")
    @Size(max = 50)
    @Column(name = "executor_route_strategy", length = 50)
    private String executorRouteStrategy;

    @NotBlank(message = "执行方法不能为空")
    @Size(max = 255)
    @Column(name = "executor_handler")
    private String executorHandler;

    @Size(max = 512)
    @Column(name = "executor_param", length = 512)
    private String executorParam = "";

    @NotBlank(message = "阻塞处理策略不能为空")
    @Size(max = 50)
    @Column(name = "executor_block_strategy", length = 50)
    private String executorBlockStrategy;

    @Column(name = "executor_timeout", nullable = false)
    private Integer executorTimeout = 0;

    @Column(name = "executor_fail_retry_count", nullable = false)
    private Integer executorFailRetryCount = 0;

    @NotBlank(message = "运行模式不能为空")
    @Size(max = 50)
    @Column(name = "glue_type", nullable = false, length = 50)
    private String glueType;

    @Lob
    @Column(name = "glue_source")
    private String glueSource = "";

    @Size(max = 128)
    @Column(name = "glue_remark", length = 128)
    private String glueRemark = "";

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "glue_updatetime")
    private Date glueUpdatetime;

    @Size(max = 255)
    @Column(name = "child_jobid")
    private String childJobid = "";

    @Column(name = "trigger_status", nullable = false)
    private Integer triggerStatus;

    @Column(name = "trigger_last_time", nullable = false)
    private Long triggerLastTime = 0L;

    @Column(name = "trigger_next_time", nullable = false)
    private Long triggerNextTime = 0L;

    /* 下次执行时间 */
    @Transient
    private List<String> nextTriggerTime;
    /* 任务执行器 */
    @Transient
    private XxlJobGroup xxlJobGroup;

    public void edit(XxlJobInfo xxlJobInfo) {
        this.jobGroup = xxlJobInfo.jobGroup;
        this.jobDesc = xxlJobInfo.jobDesc;
        this.author = xxlJobInfo.author;
        this.alarmEmail = xxlJobInfo.alarmEmail;
        this.scheduleType = xxlJobInfo.scheduleType;
        this.scheduleConf = xxlJobInfo.scheduleConf;
        this.glueType = xxlJobInfo.glueType;
        this.executorHandler = xxlJobInfo.executorHandler;
        this.executorParam = xxlJobInfo.executorParam;
        this.executorRouteStrategy = xxlJobInfo.executorRouteStrategy;
        this.misfireStrategy = xxlJobInfo.misfireStrategy;
        this.executorTimeout = xxlJobInfo.executorTimeout;
        this.childJobid = xxlJobInfo.childJobid;
        this.executorBlockStrategy = xxlJobInfo.executorBlockStrategy;
        this.executorFailRetryCount = xxlJobInfo.executorFailRetryCount;
    }

}